package com.goods.business.converter;

import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.vo.business.ProductVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProductConverter {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 转voList
     * @param products
     * @return
     */
    public  List<ProductVO> converterToProductVOList(List<Product> products){
        List<ProductVO> productVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(products)){
            for (Product product : products) {
                ProductVO productVO = converterToProductVO(product);
                productVOS.add(productVO);
            }
        }
        return productVOS;
    }

    /**
     * 转vo
     * @return
     */
    public ProductVO converterToProductVO(Product product){
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        productVO.setStatus(product.getStatus());
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(product.getId());
        if(productCategory!=null&&productCategory.getName()!=null){
            productVO.setId(product.getId());
            productVO.setName(product.getName());
        }
        return productVO;
    }


}
